<template>
  <div class="login-container">
    <h1>管理员登录</h1>
    <form class="login-form">
      <div class="form-group">
        <label for="username">用户名</label>
        <div class="input-container">
          <input type="text" id="username" v-model="username" :class="{ 'input-active': isUsernameActive }" @focus="isUsernameActive = true" @blur="checkUsername">
          <div class="input-icon">
            <i class="fa fa-user"></i>
          </div>
        </div>
        <span v-show="isUsernameActive && showUsernameTip">请输入用户名</span>
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <div class="input-container">
          <input type="password" id="password" v-model="password" :class="{ 'input-active': isPasswordActive }" @focus="isPasswordActive = true" @blur="checkPassword">
          <div class="input-icon">
            <i class="fa fa-lock"></i>
          </div>
        </div>
        <span v-show="isPasswordActive && showPasswordTip">请输入密码</span>
      </div>
      <button class="login-btn" :class="{ 'btn-active': isBtnActive }" @click.prevent="login">登录</button>
    </form>
  </div>
</template>

<script>
import axios from "axios";
import {Message} from "element-ui";

export default {
  data() {
    return {
      username: '',
      password: '',
      isUsernameActive: false,
      isPasswordActive: false,
      isBtnActive: false,
      showUsernameTip: false,
      showPasswordTip: false
    }
  },
  methods: {
    login() {
      if (!this.username || !this.password) {
        alert('请输入用户名和密码')
        return
      }
      // 登录逻辑
      // ...
      axios.get('http://localhost:8088/admin/login',{
        params:{
          username:this.username,
          password:this.password
        }
      })
          .then(response => {
            if (response.data.code==200){
              Message.success("Login successful")
              this.$router.push("/loading2")
            }else {
              Message.error("Login failed  账号或密码错误")
            }
          })
          .catch(error => {
            console.log(error)
          })
      this.isBtnActive = true
      setTimeout(() => {
        this.isBtnActive = false
      }, 1000)
    },
    checkUsername() {
      if (!this.username) {
        this.showUsernameTip = true
      } else {
        this.showUsernameTip = false
      }
    },
    checkPassword() {
      if (!this.password) {
        this.showPasswordTip = true
      } else {
        this.showPasswordTip = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  background-color: #f2f2f2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  color: #333;
}

h1 {
  text-align: center;
  font-size: 28px;
  margin-bottom: 40px;
}

.login-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  max-width: 400px;
  width: 100%;
}

.form-group {
  margin-bottom: 20px;
}

label {
  font-size: 16px;
  margin-bottom: 5px;
  display: block;
}

.input-container {
  position: relative;
}

input {
  display: block;
  width: 98%;
  padding: 12px;
  border: none;
  border-radius: 20px;
  background-color: #f5f5f5;
  transition: background-color 0.3s ease;
}

.input-active {
  background-color: #e0e0e0;
}

.input-icon {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
}

.input-icon i {
  color: #ccc;
  font-size: 16px;
}

span {
  color: red;
  font-size: 12px;
  display: block;
  margin-top: 5px;
}

.login-btn {
  display: block;
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 20px;
  background-color: #007aff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.btn-active {
  background-color: #0055a5;
}
</style>
